package com.ltxtiyu.video.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ltxtiyu.video.common.ConfigConstant;

import com.ltxtiyu.video.dao.ltx.MomentDao;
import com.ltxtiyu.video.dao.ltx.TopicDao;
import com.ltxtiyu.video.dao.ltx.VideoDao;
import com.ltxtiyu.video.entity.Topic;
import com.ltxtiyu.video.service.ms.ConfigService;
import com.ltxtiyu.video.service.ms.TopicService;
import com.ltxtiyu.video.tool.AliTool;
import com.ltxtiyu.video.tool.StringUtil;
import com.ltxtiyu.video.vo.ms.TopicVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(timeout = 6000)
public class TopicServiceImpl implements TopicService {
    @Autowired
    private TopicDao td;
    @Autowired
    private VideoDao vd;
    @Autowired
    private ConfigService configService;

    @Autowired
    private MomentDao momentDao;

    @Override
    public Map<String, Object> selectParamList(Map<String, Object> map) {

        String OSS_ENDPOINT = configService.selectByCkey(ConfigConstant.OSS_ENDPOINT).getCvalue();
        String ACCESS_ID = configService.selectByCkey(ConfigConstant.ACCESS_ID).getCvalue();
        String ACCESS_KEY = configService.selectByCkey(ConfigConstant.ACCESS_KEY).getCvalue();
        String BUCKET_ID = configService.selectByCkey(ConfigConstant.BUCKET_ID).getCvalue();
        String OSS_CDN = configService.selectByCkey(ConfigConstant.OSS_CDN).getCvalue();
        // 上传图片用OSS_ENDPOINT
        AliTool aliTool = AliTool.getInstance(OSS_ENDPOINT, ACCESS_ID, ACCESS_KEY, BUCKET_ID);
        int count = td.findByParamCount(map);
        List<TopicVo> data= td.findByParamList(map);
        List<Topic> allData =td.findAll();
        for ( TopicVo topic:data){
            String coverImageUrl = aliTool.getAccessURL(topic.getHeadPortraitId());
            String bannerImageUrl = aliTool.getAccessURL(topic.getBannerPicId());
            topic.setCoverImageUrl(coverImageUrl);
            topic.setBannerImageUrl(bannerImageUrl);
        }
        int bannerNumber = 0;
        for(Topic topic:allData){
            String bannerPicId = topic.getBannerPicId();
            if (StringUtil.isNotBlank(bannerPicId)){
                bannerNumber +=1;
            }
        }
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("msg","");
        result.put("count",count);
        result.put("data",data);
        result.put("bannerNumber",bannerNumber);
        return result;
    }

    @Override
    public void updateTopic(Topic topic) {
        List<Topic> allData =td.findAll();

        int bannerNumber = 0;
        for(Topic record:allData){
            String bannerPicId = record.getBannerPicId();
            if (StringUtil.isNotBlank(bannerPicId)){
                bannerNumber +=1;
            }
        }
//        if (bannerNumber >6){
//            topic.setBannerPicId("");
//            topic.setIsHotPush(0);
//        }
        td.updateByPrimaryKeySelective(topic);
    }

    @Override
    public void delByID(Integer id) {
        td.deleteByPrimaryKey(id);
    }
}
